package qc.module.ehs.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.module.ehs.dto.edurecord.EduRecordDto;
import qc.module.ehs.dto.edurecord.EduRecordQueryConditionDto;
import qc.module.ehs.dto.edurecord.EduRecordSetUsersDto;
import qc.module.ehs.dto.edurecord.EduRecordUsersDto;
import qc.module.ehs.dto.org.EhsOrgDto;
import qc.module.ehs.service.EhsEduRecordService;
import qc.module.ehs.service.EhsOrgService;
import qc.module.platform.dto.user.UserSimpleInfoDto;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * EduRecordController
 *
 * @author QuCheng Tech
 * @create 2024/10/29
 */
@RestController
@RequestMapping("/edurecord")
public class EduRecordController {

    @Autowired
    private EhsEduRecordService service;

    @Autowired
    private EhsOrgService orgService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplatePlatform restTemplatePlatform;

    /**
     * 查询记录
     *
     * @param condition 查询条件
     * @return List<EduRecordDto>
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @RequestMapping(value = "/query",method = {RequestMethod.GET,RequestMethod.POST})
    public List<EduRecordDto> query(@RequestBody EduRecordQueryConditionDto condition) throws QCPromptException {
        return service.query(condition);
    }

    /**
     * 获取指定记录
     *
     * @param id 记录ID
     * @return EduRecordDto
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @GetMapping("/get")
    public EduRecordDto getByParam(@RequestParam String id){
        return service.get(id);
    }

    /**
     * 获取指定记录
     * 
     * @param id 记录ID
     * @return EduRecordDto
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @GetMapping("/get/{id}")
    public EduRecordDto getByPath(@PathVariable String id){
        return service.get(id);
    }

    /**
     * 新增记录
     *
     * @param dto
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    public String add(@RequestBody EduRecordDto dto){
        return service.add(dto);
    }

    /**
     * 修改记录
     * 
     * @param dto
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @RequestMapping(value = "/update",method = {RequestMethod.POST,RequestMethod.PUT})
    public String update(@RequestBody EduRecordDto dto){
        return service.update(dto);
    }

    /**
     * 删除记录
     *
     * @param id 记录ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @DeleteMapping("/delete")
    public String deleteByParam(@RequestParam String id){
        return service.delete(id);
    }

    /**
     * 删除记录
     * 
     * @param id 记录ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @DeleteMapping("/delete/{id}")
    public String deleteByPath(@PathVariable String id){
        return service.delete(id);
    }

    /**
     * 获取记录人员关联信息
     *
     * @param id 记录ID
     * @return List<EduRecordUsersDto>
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @GetMapping("/getusers")
    public List<EduRecordUsersDto> getUsersByParam(@RequestParam String id) throws QCPromptException {
        return getEduRecordRelatedUsers(id);
    }

    /**
     * 获取记录人员关联信息
     *
     * @param id 记录ID
     * @return List<EduRecordUsersDto>
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @GetMapping("/getusers/{id}")
    public List<EduRecordUsersDto> getUsersByPath(@PathVariable String id) throws QCPromptException {
        return getEduRecordRelatedUsers(id);
    }

    /***
     * 获取指定的培训记录ID关联的用户信息集合，包含项目中的所有用户信息及是否关联该培训记录
     *
     * @param eduRecordId 培训记录ID
     * @return java.util.List<qc.module.ehs.dto.edurecord.EduRecordUsersDto>
     * @author QuCheng Tech
     * @since 2025/3/3
     */
    List<EduRecordUsersDto> getEduRecordRelatedUsers(String eduRecordId) throws QCPromptException {
        //返回的结果中需要当前组织中的所有用户，每个用户有是否关联当前培训记录
        //获取当前组织中的所有用户：从培训记录中获取组织代码，根据组织代码对应的部门ID获取所有部门集合
        if (StringUtils.isBlank(eduRecordId))
            throw new QCPromptException("指定的培训记录ID不能为空");
        EduRecordDto recordDto = service.get(eduRecordId);
        if (recordDto == null)
            throw new QCPromptException("指定的培训记录信息不存在");
        if (StringUtils.isBlank(recordDto.getOrgcode()))
            throw new QCPromptException("指定的培训记录组织代码为空");
        EhsOrgDto org = orgService.get(recordDto.getOrgcode());
        if (org == null)
            throw new QCPromptException("培训记录信息中的组织信息不存在");
        if (org.getDeptid() < 0x1)
            throw new QCPromptException("培训记录信息中的组织对应的部门ID无效");

        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));

        //获取项目用户简要信息
        List<UserSimpleInfoDto> projectUsers = restTemplatePlatform.getProjectDeptChildUserSimpleInfoList(org.getDeptid(), headers);

        return service.getUsers(eduRecordId, projectUsers);
    }

    /**
     * 设置记录人员关联
     *
     * @param dto
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @RequestMapping(value = "/setusers", method = {RequestMethod.POST})
    public String setUsers(@RequestBody EduRecordSetUsersDto dto){
        return service.setUsers(dto);
    }
}
